package com.breeze.blockchain.controller.admin;

import com.breeze.blockchain.controller.admin.form.file.FileDetailPageForm;
import com.breeze.components.core.exception.ApiException;
import com.breeze.components.core.exception.error.ErrorCode;
import com.breeze.components.core.util.DateUtil;
import com.breeze.components.oss.mybaits.entity.FileDetailDo;
import com.breeze.components.oss.service.FileDetailService;
import com.breeze.components.web.core.ResponseDTO;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件接口
 *
 * @author breeze
 * @since 2024-05-13 10:08:23
 */
@RestController
@Tag(name = "文件接口")
@RequestMapping("api/admin/file")
public class FileController {

  @Resource private FileDetailService fileDetailService;
  @Resource private FileStorageService fileStorageService;

  @Operation(summary = "附件上传")
  @PostMapping("upload")
  public ResponseDTO<FileInfo> upload(MultipartFile file) {
    FileInfo fileInfo =
        fileStorageService.of(file).setPath(DateUtil.dateFilePath("admin")).upload();
    return ResponseDTO.ok(fileInfo);
  }

  @Operation(summary = "附件下载")
  @GetMapping("download")
  public void download(@RequestParam Long id, HttpServletResponse response) throws IOException {
    FileDetailDo fileDetail = fileDetailService.getById(id);
    FileInfo fileInfo = FileDetailDo.toFileInfo(fileDetail);
    if (Objects.isNull(fileInfo) || !fileStorageService.exists(fileInfo)) {
      throw new ApiException(ErrorCode.NOT_FOUND_ERROR);
    }
    fileStorageService.download(fileInfo).outputStream(response.getOutputStream());
  }

  @Operation(summary = "附件删除")
  @GetMapping("remove")
  public ResponseDTO<Void> remove(@RequestParam Long id) {
    fileStorageService.delete(FileDetailDo.toFileInfo(fileDetailService.getById(id)));
    return ResponseDTO.ok();
  }

  @GetMapping("detail")
  @Operation(summary = "文件记录详情")
  public ResponseDTO<FileDetailDo> getInfo(@RequestParam @Parameter(name = "文件记录表主键") Long id) {
    return ResponseDTO.ok(fileDetailService.getById(id));
  }

  @PostMapping("page")
  @Operation(summary = "分页查询文件记录")
  public ResponseDTO<Page<FileDetailDo>> page(@RequestBody FileDetailPageForm request) {
    return ResponseDTO.ok(fileDetailService.page(request.toPage(), request.toQueryWrapper()));
  }
}
